package com.ntanh.mealplanner.main.dao;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.ntanh.mealplanner.base.model.BaseEntity;
import com.ntanh.mealplanner.main.objects.DayMeal;
import com.ntanh.mealplanner.main.objects.Meal;
import com.ntanh.mealplanner.main.objects.WeekMeal;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Anh Nguyen on 2/27/2015.
 */
public class DayMealDao extends BaseDao {

    public DayMealDao(Context context) {
        super(context);
    }

    public ArrayList<DayMeal> getAll(String author, boolean lazy) {
        ArrayList<DayMeal> dayMeals = new ArrayList<DayMeal>();
        MealDao mealDao = new MealDao(mContext);
        String query = "SELECT " + "*"
                + " FROM " + DataBaseHelper.DAY_MEAL_TABLE;

        Log.d("query", query);
        Cursor cursor = database.rawQuery(query, null);
        while (cursor.moveToNext()) {
            DayMeal dayMeal = convertFromCursor(cursor,author,mealDao, lazy);
            dayMeals.add(dayMeal);
        }
        return dayMeals;
    }

    public DayMeal getById(String author, Integer id) {
        MealDao mealDao = new MealDao(mContext);
        DayMeal dayMeal = null;
        String query = "SELECT " + "*"
                + " FROM " + DataBaseHelper.DAY_MEAL_TABLE
                + " WHERE id = " + id;

        Log.d("query", query);
        Cursor cursor = database.rawQuery(query, null);
        while (cursor.moveToNext()) {
            dayMeal = convertFromCursor(cursor,author,mealDao, false);
        }
        return dayMeal;
    }

    public DayMeal getByDate(String author, Date date) {
        MealDao mealDao = new MealDao(mContext);
        DayMeal dayMeal = null;
        String query = "SELECT " + "*"
                + " FROM " + DataBaseHelper.DAY_MEAL_TABLE
                + " WHERE " + DayMeal.DATE + " = '" + BaseEntity.dateFormatter.format(date) + "'";

        Log.d("query", query);
        Cursor cursor = database.rawQuery(query, null);
        while (cursor.moveToNext()) {
            dayMeal = convertFromCursor(cursor,author,mealDao, false);
        }
        return dayMeal;
    }

    private DayMeal convertFromCursor(Cursor cursor, String author, MealDao mealDao, boolean lazy) {
        DayMeal dayMeal = new DayMeal(author);
        dayMeal.setId(cursor.getInt(0));
        try {
            dayMeal.setCreatedDate(BaseEntity.dateFormatter.parse(cursor.getString(1)));
        } catch (ParseException e) {
            dayMeal.setCreatedDate(null);
        }
        try {
            dayMeal.setModifiedDate(BaseEntity.dateFormatter.parse(cursor.getString(2)));
        } catch (ParseException e) {
            dayMeal.setModifiedDate(null);
        }
        dayMeal.setCreatedBy(cursor.getString(3));
        dayMeal.setModifiedBy(cursor.getString(4));
        dayMeal.setActive(cursor.getInt(5) == 1);
        dayMeal.setDeleted(cursor.getInt(6) == 1);
        dayMeal.setSynced(cursor.getInt(7) == 1);
        if (!lazy) {
            String mealStr = cursor.getString(8);
            if(mealStr != null) {
                if (mealStr.contains(BaseEntity.entitySplitSign)) {
                    String[] array = mealStr.split(BaseEntity.entitySplitSign);
                    for (String mealId : array) {
                        if (!mealId.isEmpty()) {
                            Meal meal = mealDao.getById(author, Integer.parseInt(mealId));
                            if (meal != null) {
                                dayMeal.getMealList().add(meal);
                            }
                        }
                    }
                } else if (!mealStr.isEmpty()) {
                    Meal meal = mealDao.getById(author, Integer.parseInt(mealStr));
                    if (meal != null) {
                        dayMeal.getMealList().add(meal);
                    }
                }
            }
        }
        try {
            dayMeal.setDate(BaseEntity.dateFormatter.parse(cursor.getString(9)));
        } catch (ParseException e) {
            dayMeal.setDate(null);
        }

        return dayMeal;
    }
}
